package com.example.music;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.example.music.Mine.MineFragment;
import com.example.music.entity.User;
import com.example.music.sql.DBOpenHelper;

import java.util.ArrayList;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private DBOpenHelper mDBOpenHelper;
    private TextView lg_Register;
    private EditText lg_username,lg_password;
    private CheckBox isRemember;
    private Button lg_button1;
    private String username,password,sp_name,sp_pwd;
    private Boolean sp_isRemember;//是否记住密码的标志
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private Context mContext;


    public static void launch(Context context){
        context.startActivity(new Intent(context,LoginActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_longin);
        initView();
        mDBOpenHelper = new DBOpenHelper(this);

        //读取缓存数据
        sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        sp_name=sp.getString("username","");
        sp_pwd=sp.getString("password","");
        sp_isRemember=sp.getBoolean("isRemember",false);
        //如果有账号，直接显示
        Log.d("TAG",sp_name);
        lg_username.setText(sp_name);
        //如果上次登录记住了密码，本次登录直接设置上
        if(sp_isRemember.equals(true)){
            lg_password.setText(sp_pwd);
            isRemember.setChecked(true);
        }
        mContext= LoginActivity.this;
    }

    private void initView() {
        //初始化控件
        lg_Register = (TextView) findViewById(R.id.lg_Register);
        lg_button1 = (Button) findViewById(R.id.lg_button1);
        lg_username = (EditText) findViewById(R.id.lg_username);
        lg_password = (EditText) findViewById(R.id.lg_password);
        isRemember=findViewById(R.id.isRemember);
        //点击事件
        lg_Register.setOnClickListener(this);
        lg_button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //跳转注册界面
            case R.id.lg_Register:
                startActivity(new Intent(this, RegisterActivity.class));
                finish();
                break;
            //跳转登录界面
            case R.id.lg_button1:
                //登录验证：从EditText的对象上获取文本编辑框输入的数据，并把左右两边的空格去掉
                username = lg_username.getText().toString().trim();
                password = lg_password.getText().toString().trim();
                //进行匹配验证,先判断一下用户名密码是否为空，
                // TextUtils.isEmpty判断是否输入
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(mContext, "请输入账号", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(password)) {
                    Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
                    // 判断，输入的账号密码，是否与保存在SharedPreferences中一致
                } else if (username.equals(sp_name) && password.equals(sp_pwd)) {
                    //一致登录成功
                    Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
                    //判断记住密码按钮是否选中,选中则将isRemember设置为true，保存状态
                    sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
                    //获取编辑器
                    editor=sp.edit();
                    if (isRemember.isChecked()) {//如果记住密码选中，存入true,否则存入false
                        editor.putBoolean("isRemember",true);
                    }else {
                        editor.putBoolean("isRemember",false);
                    }
                    editor.putBoolean("isLogin",true);//保存登录状态
                    editor.apply();//提交修改
                    //登录成功后关闭此页面进入主页

                    //跳转到主界面，登录成功的状态传递到 MainActivity 中
                    Intent intent=new Intent(getApplicationContext(), MainActivity.class);
                    intent.putExtra("name",username);
                    startActivity(intent);
                    finish();//销毁登录界面
                } else if ((sp_pwd != null && !TextUtils.isEmpty(sp_pwd) && !password.equals(sp_pwd))) {
                    Toast.makeText(mContext, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "此用户名不存在", Toast.LENGTH_SHORT).show();
                }
        }
    }

}



